<template>
    <el-dialog title="分配用户" destroy-on-close :close-on-click-modal="false"
        :visible.sync="visible" append-to-body>

        <div style="padding:20px" v-if="visible">
            <el-tabs v-model="activeName" @tab-click="handleClick">
                <el-tab-pane label="选择用户" name="first">

                </el-tab-pane>
                <el-tab-pane label="新增用户" name="second">
                    <el-form :model="dataForm" :rules="dataRule" ref="dataForm"
                        label-width="120px">

                        <ec-form-item-header value="用户基本信息"></ec-form-item-header>
                        <el-row :gutter="15">
                            <el-col :span="12">
                                <el-form-item label="姓名" prop="username">
                                    <el-input clearable v-model="dataForm.username" placeholder="用户名" maxlength="10"
                                        show-word-limit></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="身份证号" prop="idCard">
                                    <el-input clearable v-model="dataForm.idCard" placeholder="身份证号" maxlength="18"
                                        show-word-limit>
                                    </el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row :gutter="15">
                            <el-col :span="12">

                                <el-form-item label="手机号" prop="phone">
                                    <el-input clearable v-model="dataForm.phone" placeholder="手机号" maxlength="11"
                                        show-word-limit>
                                    </el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <!-- <el-form-item label="头像" prop="avatar">
                        <el-input clearable v-model="dataForm.avatar" placeholder="头像"></el-input>
                    </el-form-item> -->
                            </el-col>
                        </el-row>
                        <el-row :gutter="15">
                            <el-col :span="12">
                                <el-form-item label="邮箱" prop="email">
                                    <el-input clearable v-model="dataForm.email" placeholder="邮箱" maxlength="20"
                                        show-word-limit>
                                    </el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>

                        <ec-form-item-header value="用户业务信息"></ec-form-item-header>
                        <el-row :gutter="15">
                            <el-col :span="12">
                                <el-form-item label="所属集团" prop="pkGroup">
                                    <ec-group-tree-select :visible="visible" v-model="dataForm.pkGroup"
                                        placeholder="请选择所属集团" :disabled="true">
                                    </ec-group-tree-select>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                </el-tab-pane>
            </el-tabs>
        </div>
        <span slot="footer" class="dialog-footer">
            <el-button @click="closeDailog()">取消</el-button>
            <el-button type="primary" :loading="submitLoading" @click="dataFormSubmit()">确定</el-button>
        </span>



    </el-dialog>
</template>
<script>
    import * as userService from '@/api/upms/user'
    import * as dictService from '@/api/bdms/dict'
    import * as orgService from '@/api/upms/org'
    import * as groupService from '@/api/upms/group'
    export default {
        data() {
            var checkPhone = (rule, value, callback) => {
                if (!value) {
                    return callback(new Error('手机号不能为空'));
                } else {
                    const reg = /^1[3|4|5|7|8][0-9]\d{8}$/
                    if (reg.test(value)) {
                        callback();
                    } else {
                        return callback(new Error('请输入正确的手机号'));
                    }
                }
            }
            return {
                visible: false,
                submitLoading: false,
                activeName: 'second',
                dataForm: {
                    userId: 0,
                    username: '',
                    accountName: '',
                    password: '',
                    phone: '',
                    idCard: '',
                    email: '',
                    userType: 'sys',
                    avatar: '',
                    lockFlag: '',
                    delFlag: '',
                    revision: '',
                    pkGroup: '',
                    pkOrg: '',
                    pkDept: '',
                },
                dataRule: {
                    pkGroup: [{
                        required: true,
                        message: '所属集团不能为空',
                        trigger: 'blur'
                    }],
                    username: [{
                        required: true,
                        message: '姓名不能为空',
                        trigger: 'blur'
                    }],
                    idCard: [{
                        required: true,
                        message: '身份证不能为空',
                        trigger: 'blur'
                    }],
                    phone: [{
                        required: true,
                        validator: checkPhone,
                        message: '手机号不能为空',
                        trigger: 'blur'
                    }],
                    email: [{
                        required: true,
                        message: '邮箱号不能为空',
                        trigger: 'blur'
                    }],
                }
            }
        },
        components: {},
        methods: {
            init(id, pkGroup) {
                this.dataForm.userId = id || 0
                this.dataForm.pkGroup = pkGroup
                this.visible = true
            },
            // 表单提交
            dataFormSubmit() {
                this.$refs['dataForm'].validate((valid) => {
                    if (valid) {
                        this.submitLoading = true
                        var params = {
                            'userId': this.dataForm.userId || undefined,
                            'pkGroup': this.dataForm.pkGroup,
                            'pkOrg': this.dataForm.pkOrg,
                            'username': this.dataForm.username,
                            'accountName': this.dataForm.accountName,
                            'password': this.dataForm.password,
                            'salt': this.dataForm.salt,
                            'phone': this.dataForm.phone,
                            'idCard': this.dataForm.idCard,
                            'email': this.dataForm.email,
                            'userType': this.dataForm.userType,
                            'avatar': this.dataForm.avatar,
                            'lockFlag': this.dataForm.lockFlag,
                            'delFlag': this.dataForm.delFlag,
                            'revision': this.dataForm.revision,
                            'pkDept': this.dataForm.pkDept,
                            'pkGroup': this.dataForm.pkGroup,
                            'pkOrg': this.dataForm.pkOrg,
                        }
                        var tick = !this.dataForm.userId ? userService.addAdmin(params) : userService
                            .updateAdmin(params)
                        tick.then(data => {
                            this.$message({
                                message: '操作成功',
                                type: 'success',
                                duration: 1500,
                                onClose: () => {
                                    this.submitLoading = false
                                    this.closeDailog()
                                    this.$emit('refreshDataList')
                                }
                            })
                        }).catch((res) => {
                            this.submitLoading = false
                            this.closeDailog()
                        })
                    }
                })
            },
            closeDailog() {
                console.log("关闭")
                // this.$refs['dataForm'].resetFields()
                this.visible = false
            },
            handleClick(tab, event) {
                console.log(tab, event);
            }
        }
    }
</script>